class HomeController < ApplicationController
  skip_before_filter :verify_authenticity_token

  def index
    @count = 20
    @top_sideffects = Sideffect.top(@count)
    @top_drugs = Drug.top(@count)
    @num_drugs = Drug.count
    @num_ratings = Rating.count
    @page_title = 'Home'
  end
  
  def auto_complete_for_search_name
    @matched_drug_names = Drug.with_prefix(params[:search][:name])
    @matched_category_names = Category.with_prefix(params[:search][:name])
    render :partial => 'auto_complete_name', :layout => false
  end
  
  def switch_initials_list
    @is_show_category_initials = (params[:which_initials] == "category")
    render :partial => 'initials_list', :layout => false
  end
  
  def search
    drug = Drug.find_by_name(params[:search][:name])
    if drug.blank?
      category = Category.find_by_name(params[:search][:name])
      redirect_to :controller => 'category', :action => 'show', :id => category.id unless category.blank?
    else
      redirect_to :controller => 'drug', :action => 'show', :id => drug.id
    end
  end

end
